<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class BuyContractTemplate extends BaseController
{
    protected $title='销售合同模板';
    protected $table='contract_template';
    protected $scType=2;
    public function index()
    {
        $param=$this->request->param();
        if (request()->isAjax()) {
            $where[]=['ct.status','>',-1];
            $where[]=['ct.type','=',$this->scType];

            if (isset($this->param['name'])&&$this->param['name']){
                $where[]=['py.name|ad.name|ct.name','like','%'.$this->param['name'].'%'];
            }
            if (isset($this->param['orderDate'])&&$this->param['orderDate']){
                $dates=explode('~',$this->param['orderDate']);
                $b_tiem=strtotime($dates[0]);
                $e_time=strtotime($dates[1].' 23:59:59');
                $where[]=['ct.create_time','between',[$b_tiem,$e_time]];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->alias('ct')
                ->leftJoin('purveyor py','py.id=ct.purveyorId')
                ->leftJoin('admin ad','ad.id=ct.createId')
                ->where($where)
                ->field('ct.*,py.name as pName,ad.name as upName,(select name from fy_file f where f.filepath= ct.files limit 1) fileName')
                ->order('ct.id DESC')
                ->paginate($rows)->each(function ($item){
//                    if ($item['files']){
//                        $item['files']=Db::name('file')->where('filepath',$item['files'])->field('name,filepath')->select();
//                    }else{
//                        $item['files']=[];
//                    }
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }

    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            Db::StartTrans();
            try {
                $param=$this->param;
                if (!$param['purveyorId']){
                    throw new Exception('请选择仓库');
                }
                unset($param['file']);
                $param['update_time']=time();
                if ($param['id'] > 0) {
                    Db::name($this->table)->strict(false)->field(true)->update($param);
                    add_log('edit', $param['id'], $param,$this->title);
                } else {
                    $param['create_time'] = time();
                    $param['type']=$this->scType;
                    $param['createId']=$this->uid;
                    $param['departId']=$this->did;
                    $rid = Db::name($this->table)->insertGetId($param);
                    add_log('add', $rid, $param,$this->title);
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                return to_assign(1,$e->getMessage());
            }

            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            $detail=[];
            if ($id > 0) {
                $detail = Db::name($this->table)->where('id',$id)->find();
            }
            $punterList=Db::name('purveyor')->where('status',1)->where('dw_type',1)->field('id,name')->select();
            View::assign('punterList', $punterList);
            View::assign('detail', $detail);
            View::assign('id', $id);
            return view('form');
        }
    }

    public function delete()
    {
        if (request()->isDelete()) {
            $id = get_params("id");
            try {
                Db::name($this->table)->where('id','in',$id)->update(['status'=>-1,'deleted'=>time()]);
                add_log('delete', $id, request()->param(),$this->title);
            }catch (\Exception $e){
                return to_assign(1, "删除失败".$e->getMessage());
            }
            return to_assign(0, "删除成功");
        } else {
            return to_assign(1, "错误的请求");
        }
    }
}